import Vue from "vue"
import Router from "vue-router"
import store from "@/store"
import trade from "./trade/index.js"
import portal from "./portal"
import oauth from "./oauth"
import {getTabIndex} from "@/modules/transition"
import RouterMiddleware from '@/modules/middleware'
import middlewares from "@/middlewares";

Vue.use(Router)

let routes = trade.concat(portal)
routes = routes.concat(oauth)

const router = new Router({
  routes
})

const tabKeywordsArray = ['portal', 'breed', 'market', 'trade', 'oauth']

//挂载中间件
router.beforeEach((to, from, next) => {
  //对底栏进行处理
  store.commit('hideTab')
  store.commit('setTabIndex', getTabIndex(tabKeywordsArray)(to.path)) 
  //保留一个from快照，避免未mounted的页面得不到from
  store.commit('updateRouterFrom', from)
  new RouterMiddleware(middlewares).use({to, from, next})
})

export default router
